Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

202
Visualizações
How would a switch statement look like which has to express a complex condition that contains both logical operators (|| and &&)?

I have to build a condition based on one or two values.

const getLabel = (type?: string, typeXy?: string) => {
  let label;

  switch (type || typeXy) {
    case 'Value A':
      label = 'Label A';
      break;
    case 'Value B':
      label = 'Label B';
      break;
    case 'Value C':
    case 'Value X':
      label = 'Label C + X';
      break;
    case 'Value Y':
      label = 'Label C + Y';
      break;
    default:
      break;
  }
  return label;
};

I need a dubbel case on two places. When I add case 'Value C': above case 'Value Y': I get:

Duplicate case label.eslintno-duplicate-case

1) I add // eslint-disable-next-line no-duplicate-case but my code isn't working yet. Or is there a cleaner way anyway?

2) I can change my switch from the or to the and: (type || typeXy) but then the first case isn't working? How do I support both or and and?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

How about using an object literal as lookup table where one can precisely express with, which result for which case one does prefer ?..

const getLabel = (type/*?: string*/, typeXy/*?: string*/) => {
  return ({

    'Value A': 'Label A',
    'Value B': 'Label B',
    'Value C': 'Label C',
    'Value X': 'Label X',
    'Value Y': 'Label Y',
    'Value CValue X': 'Label C + X',
    'Value XValue C': 'Label C + X',
    'Value CValue Y': 'Label C + Y',
    'Value YValue C': 'Label C + Y',

  })[(type ?? '') + (typeXy ?? '')];
};

console.log("getLabel('Value A') ...", getLabel('Value A'));

console.log("getLabel('Value B', null) ...", getLabel('Value B', null));
console.log("getLabel(null, 'Value C') ...", getLabel(null, 'Value C'));
console.log("getLabel(undefined, 'Value X') ...", getLabel(undefined, 'Value X'));

console.log("getLabel('Value Y') ...", getLabel('Value Y'));

console.log("getLabel('Value C', 'Value X') ...", getLabel('Value C', 'Value X'));
console.log("getLabel('Value X', 'Value C') ...", getLabel('Value X', 'Value C'));

console.log("getLabel('Value C', 'Value Y') ...", getLabel('Value C', 'Value Y'));
console.log("getLabel('Value Y', 'Value C') ...", getLabel('Value Y', 'Value C'));
.as-console-wrapper { min-height: 100%!important; top: 0; }

about 4 years ago · Juan Pablo Isaza Relatório

0

If I understand your question you want to conditionally check if both arguments are passed and if so, use a logical AND comparison, otherwise if only one or the other is passed revert to a logical OR comparison.

Use an outer switch to branch on the logical AND/OR condition, and then use nested switches to evaluate the specific AND or OR combinations.

const getLabel = (type /*?: string*/, typeXy /*?: string*/) => {
  switch (true) {
    case !!(type && typeXy):
      switch (type) {
        case "Value C": {
          let label = "Label C";
          switch (typeXy) {
            case "Value X":
              return label + " + X";
            case "Value Y":
              return label + " + Y";
          }
        }
      }
      break;

    case !!(type || typeXy):
      switch (type || typeXy) {
        case "Value A":
          return "Label A";
        case "Value B":
          return "Label B";
      }
  }
};

console.log(getLabel("Value A"));            // Label A
console.log(getLabel("Value B"));            // Label B
console.log(getLabel(undefined, "Value A")); // Label A
console.log(getLabel(undefined, "Value B")); // Label B
console.log(getLabel("Value C", "Value X")); // Label C + X
console.log(getLabel("Value C", "Value Y")); // Label C + Y

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda